##################################################################

# Gautam Nair
# gautam.nair@yale.edu
# Misperceptions of Relative Affluence and Support for International Transfers
# Make Table 12: Correlation between Prior Giving and Giving in the Experiment (among control group respondents)

##################################################################

# setwd("")

##################################################################
# loading packages
##################################################################

library(Hmisc)
library(foreign)
library(sandwich)
library(lmtest)
library(numDeriv)
library(stargazer)
library(ggplot2)
library(plyr)
library(gridExtra)
library(ri)
library(dplyr)
library(plyr)
library(scales)

##################################################################

rm(list=ls())

data.working <- readRDS("d_r_cleaned_analysis_dataset.Rda")

data.working <- data.working[data.working$group1==1,]

vars1 <- c(
"charity.pretreat.yes")

vars2 <- c(
"charity.pretreat.yes",
"income.new",
"age",
"education.categorical",
"ideo.new",
"identity.cosmopolitan",
"partyid",
"religion.attendance",
"work.employed",
"gender.female"
)

varsomit <- c(
"income.new",
"age",
"education.categorical",
"ideo.new",
"identity.cosmopolitan",
"partyid",
"religion.attendance",
"work.employed",
"gender.female"
)

list.ind.group <- list(vars1, vars2)

dep.var <- c("q13ab.positive", "q13ab.2.3.total")
reg.models <-vector("list", length(dep.var)) 
se.models <-vector("list", length(dep.var))

x=0 

for(y in 1:length(list.ind.group)){
	for(i in 1:length(dep.var)){
		my.formula <-paste(dep.var[i],'~', paste(list.ind.group[[y]], collapse= ' + '))
		temp.model <- lm(my.formula, data=data.working)
		temp.se <- coeftest(temp.model, vcov=vcovHC(temp.model,type="HC2"))[,2]
		x = x+1
		se.models[[x]] <- temp.se
		reg.models[[x]] <- temp.model
	}
}

varlabels <-       c("Previous Charitable Giving (1/0)")
spectitle <- c("Correlation between Charitable Giving in Study and Prior Charitable Giving")
outputfile <- c("tf_t_12_prior_giving_giving_experiment_correlation.txt")
columnlabels <-c("Positive Donation (1/0)", "Perc. Donated", "Positive Donation (1/0)", "Perc. Donated")
depvarcaption <- c("Charitable Giving")
cutoffs.star <- c(0.05, 0.01)
char.star = c("**", "***")
lines.add <- c("Controls", "N", "N", "Y", "Y")

stargazer(reg.models[[1]],  reg.models[[2]], reg.models[[3]], reg.models[[4]], 
          se=list(se.models[[1]],  se.models[[2]], se.models[[3]], se.models[[4]]),
         title= spectitle,
          out=outputfile,
          type="text",
          no.space=TRUE, 
          model.numbers= TRUE,
         notes.append=FALSE,
          align=FALSE,
         covariate.labels=varlabels,
          header= FALSE,
          font.size="small",
          model.names=TRUE,
          digits=2,
          omit.stat = c("rsq", "f","adj.rsq"),
          column.labels  = columnlabels,
          dep.var.caption  = "",
          dep.var.labels.include=FALSE,
         df=FALSE,
         omit.table.layout="n",
                  star.cutoffs = cutoffs.star,
     star.char = char.star,
         add.lines=list(lines.add),
         omit= varsomit
)   
